package cn.jiyun.service.impl;

import cn.jiyun.model.pojo.Address;
import cn.jiyun.service.IAddressService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.jiyun.mapper.AddressMapper;
import cn.jiyun.model.query.AddressQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import java.util.List;

/**
 * 针对表【-Address】的业务操作的Service实现类
 *
 * @author
 * @since 2024-12-18
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {

    @Autowired
    private AddressMapper addressMapper;
    @Override
    public IPage<Address> getPageList(AddressQuery query){
        //1.创建查询条件
        QueryWrapper<Address> qw = new QueryWrapper<>();
        //ToDo 查询条件

        //2.组件分页数据
        IPage<Address> page = new Page<>(query.getCurrentPage(), query.getPageSize());
        //3.查询
        baseMapper.selectPage(page,qw);
        //4.返回
        return page;
    }

    @Override
    public List<Address> showlist(Integer id) {
        QueryWrapper<Address> qw = new QueryWrapper<>();
        qw.eq("id",id);
        return addressMapper.selectList(qw);
    }

    @Override
    public Address getDefaultAddress(Integer id) {
        // 假设默认地址通过某个字段标识，例如 isDefault = true
        return addressMapper.selectOne(new QueryWrapper<Address>()
                .eq("id", id)
                .eq("is_default", true));
    }

    @Override
    public void updateDefaultAddress(Integer id, Long addressId) {
        addressMapper.updateDefaultAddress0(id, addressId);
        addressMapper.updateDefaultAddress1(id, addressId);
    }

    @Override
    public void delete(Long id) {
        addressMapper.del(id);
    }
}
